import { http, Response } from "@/api/http";

export interface LoginData {
  username: string
  password: string
  imageCode?: string
  deviceId?: string
}

// 登录接口
export function login(loginData: LoginData): Promise<Response<any>> {
  const { username, password, imageCode, deviceId } = loginData;
  const headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Basic eWFuZ21hb2xpbjp5YW5nbWFvbGlu',
    'type': 0,
    'deviceId': deviceId
  }

  const data = {
    username: username,
    password: password,
    imageCode: imageCode
  }

  return http({
    url: '/jwcloud/login',
    method: 'POST',
    data,
    headers
  })
}

// 获取验证码
export function createCode(): Promise<Response> {
  return http({
    url: '/jwcloud/system/createCode',
    method: 'GET'
  })
}

// 获取加密密钥
export function getRsaKey(account: string): Promise<Response> {
  return http({
    url: '/jwcloud/system/findPublicKey',
    method: 'POST',
    data: { account }
  })
}

/**
 * 获取用户信息
 */
export function getUserInfo(): Promise<Response> {
  return http({
    url: '/jwcloud/system/findCurrentUserDetail',
    method: 'GET',
  });
}

/**
 * 获取用户列表
 */
export function getUserList(data: any): Promise<Response> {
  return http({
    url: '/jwcloud/system/findUser',
    method: 'POST',
    data
  })
}

/**
 * 强制用户重置密码
 * @param data 
 * @returns 
 */
export function resetPassword(data: { id: any }): Promise<Response> {
  return http({
    url: '/jwcloud/system/resetPassword',
    method: 'POST',
    data
  })
}

/**
 * 根据id获取用户详细信息
 * @param data 
 * @returns 
 */
export function findUserDetail(data: { id: number }) {
  return http({
    url: '/jwcloud/system/findUserDetail',
    method: 'POST',
    data
  })
}

/**
 * 修改用户信息
 * @param data 
 * @returns 
 */
export function updateUser(data: any): Promise<Response> {
  data.orgModelWrapper = []
  for (let i in data.orgIds) {
    const orgId = data.orgIds[i]
    data.orgModelWrapper.push({
      id: orgId
    })
  }
  return http({
    url: `/jwcloud/system/updateUser?roleId=${data.roleId}`,
    method: 'POST',
    data
  })
}

/**
 * 退出登录
 * @returns 
 */
export function doLogout(): Promise<Response> {
  return http({
    url: `/jwcloud/logout`,
    method: 'POST',
    data: {}
  })
}

// 校验token是否合法
export function checkToken() {
  return http({
    url: `/jwcloud/system/checkToken`,
    method: 'GET',
  })
}

export default {
  login,
  doLogout,
  createCode,
  getRsaKey,
  getUserInfo,
  getUserList,
  resetPassword,
  findUserDetail,
  updateUser,
  checkToken,
}